
function buildMultiOptions(dataSet, chartType, name, queryType) {
    const ds = dataSet.data;
    let names;
    if(queryType === 'CATEGORY') {
        names = dataSet.cateList;
    }else if (queryType === 'COUNTRY') {
        names = dataSet.countries;
    }
    if (!chartType) {
        chartType = 'line'
    }
    let legend = {
        data: names,
        show: true,
        left: 'left'
    };
    var series = [];
    for (var i = 0; i < 4; i++) {
        let sumCheckFunc = (sums, c) => {
            let tempList = sums.filter( s => s.name === c)
            if (tempList.length === 1) {
                return tempList[0].sum
            }
            return 0;
        };
        for (var j = 0; j < names.length; j ++) {
            let c = names[j];
            series.push({
                name: c + ds[i].first,
                type: chartType,
                xAxisIndex: i,
                yAxisIndex: i,
                data: ds[i].second.map(o => sumCheckFunc(o.sums, c)),
                stack: c
            })
        }
    }
    return {
        title: [
            {
                text: `${name} 数据分析`,
                x: 'center',
                y: 0,
                bottom: 20
            },
            {
                text: ds[0].first,
                x: '14%',
                y: '7%',
                bottom: 20
            },{
                text: ds[1].first,
                x: '52%',
                y: '7%',
                bottom: 20
            },
            {
                text: ds[2].first,
                x: '14%',
                y: '52%',
                bottom: 20
            },{
                text: ds[3].first,
                x: '52%',
                y: '52%',
                bottom: 20
            }
        ],
        tooltip: {
            formatter: '{a} {b} : {c}'
        },
        grid: [
            {left: '14%', top: '14%', width: '31%', height: '31%', containLabel: true},
            {left: '52%', top: '14%', width: '35%', height: '31%', containLabel: true},
            {left: '14%', top: '59%', width: '31%', height: '31%', containLabel: true},
            {left: '52%', top: '59%', width: '35%', height: '31%', containLabel: true}
        ],
        xAxis: [
            {gridIndex: 0, type: 'category', data: dataSet.years},
            {gridIndex: 1, type: 'category', data: dataSet.years},
            {gridIndex: 2, type: 'category', data: dataSet.years},
            {gridIndex: 3, type: 'category', data: dataSet.years}
        ],
        yAxis: [
            {gridIndex: 0},
            {gridIndex: 1},
            {gridIndex: 2},
            {gridIndex: 3}
        ],
        series: series,
        legend: legend
    };
}

function buildSingleOptions(dataSet, chartType, name) {
    return {
        title: [
            {
                text: `${name} 数据分析`,
                x: 'center',
                y: 0,
                bottom: 20
            },
            {
                text: dataSet[0].first,
                x: '14%',
                y: '7%',
                bottom: 20
            },{
                text: dataSet[1].first,
                x: '52%',
                y: '7%',
                bottom: 20
            },
            {
                text: dataSet[2].first,
                x: '14%',
                y: '52%',
                bottom: 20
            },{
                text: dataSet[3].first,
                x: '52%',
                y: '52%',
                bottom: 20
            }
            ],
        tooltip: {
          formatter: '{a} {b} : {c}'
        },
        dataZoom: [
            {type: 'inside',
                xAxisIndex: [0, 1, 2, 3],
                // yAxisIndex: 0,
                startValue: 0, endValue: 4},
            // {type: 'inside', xAxisIndex: 1, yAxisIndex: 1, startValue: 0, endValue: 4},
            // {type: 'inside', xAxisIndex: 2, yAxisIndex: 2, startValue: 0, endValue: 4},
            // {type: 'inside', xAxisIndex: 3, yAxisIndex: 3, startValue: 0, endValue: 4}
        ],
        grid: [
            {left: '14%', top: '14%', width: '31%', height: '31%', containLabel: true},
            {left: '52%', top: '14%', width: '35%', height: '31%', containLabel: true},
            {left: '14%', top: '59%', width: '31%', height: '31%', containLabel: true},
            {left: '52%', top: '59%', width: '35%', height: '31%', containLabel: true}
        ],
        xAxis: [
            {gridIndex: 0, type: 'category', data: dataSet[0].second.map(o => o.name)},
            {gridIndex: 1, type: 'category', data: dataSet[1].second.map(o => o.name)},
            {gridIndex: 2, type: 'category', data: dataSet[2].second.map(o => o.name)},
            {gridIndex: 3, type: 'category', data: dataSet[3].second.map(o => o.name)}
        ],
        yAxis: [
            {gridIndex: 0},
            {gridIndex: 1},
            {gridIndex: 2},
            {gridIndex: 3}
        ],
        series: [
            {
                title: {
                    text: dataSet[0].first
                },
                name: dataSet[0].first,
                type: chartType,
                xAxisIndex: 0,
                yAxisIndex: 0,
                data: dataSet[0].second.map(o => o.sum)
            },
            {
                name: dataSet[1].first,
                type: chartType,
                xAxisIndex: 1,
                yAxisIndex: 1,
                data: dataSet[1].second.map(o => o.sum)
            },
            {
                name: dataSet[2].first,
                type: chartType,
                xAxisIndex: 2,
                yAxisIndex: 2,
                data: dataSet[2].second.map(o => o.sum)
            },
            {
                name: dataSet[3].first,
                type: chartType,
                xAxisIndex: 3,
                yAxisIndex: 3,
                data: dataSet[3].second.map(o => o.sum)
            }
        ]
    };
}

export default {
    buildSingleOptions,
    buildMultiOptions
}